<?php

/*
Zoo Management Simulation
This file provides editing functionality for the cage entry which is continued from the editCage file.
It also updates the database with data from the previous form in the editCage file.
*/

include("dbconnect.php");
if ($_REQUEST['submit'] == "Return to Administator page"){
	header('Location: setupAdmin.php');
}

$cageid = $_REQUEST['cageid'];

if ($_REQUEST['submit'] == "Edit Cage"){
	$sql = "UPDATE `enclosure` SET `enclosureName`='".$_REQUEST['name']."',`enclosureSize`=".$_REQUEST['size'].",`enclosureTypeId`=".$_REQUEST['type'].",`exhibitName`='".$_REQUEST['exhibitname']."',`exhibitDesc`='".$_REQUEST['exhibitdesc']."', `numEnclosureEntries`=".$_REQUEST['numentry'].", `enclosureHasAnimal`=".$_REQUEST['animaldetector'].", `enclosureHasPerson`=".$_REQUEST['humandetector']." WHERE enclosureId=".$cageid;
	
	$dbh->exec($sql);
}
$numentry = $_REQUEST['numentry'];
$numberofEntries = 0;
$entrysql = "SELECT entryId FROM entry WHERE enclosureId=".$cageid;
foreach ($dbh->query($entrysql) as $entryrow){
	$numberofEntries+=1;
}

if ($numentry < $numberofEntries){
while ($numentry < $numberofEntries){
	$maxsql = "SELECT max(`entryId`) FROM entry WHERE enclosureId =".$cageid;
		foreach($dbh->query($maxsql) as $row)
		{$deletesql = "DELETE FROM entry WHERE entryId=".$row[0];
		$dbh->exec($deletesql);
		}
	$numberofEntries-=1;
}}

elseif ($numentry > $numberofEntries){
while ($numentry > $numberofEntries){
	$maxsql = "SELECT max(`entryId`)+1 FROM entry WHERE enclosureId =".$cageid;
	foreach($dbh->query($maxsql) as $row)
		{	$newid = $row[0];
			$insertsql = "INSERT INTO `entry`(`entryId`, `enclosureId`, `entryTypeId`, `isEntryOpen`) VALUES (".$newid.",".$cageid.",1,0)";
			$dbh->exec($insertsql);;
}
	$numberofEntries+=1;
}}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Cage Entries</title>
<link href="mainstyle.css" rel="stylesheet" type="text/css" />
</head>

<body>
<h1>Edit Cage Entries</h1>

<form id="editCageEntryForm" name="editCageEntryForm" method="post" enctype="multipart/form-data" action="editCageEntryProcess.php">
<table id="zootable" width="100%" border="1">
	  <tr>
	    <td id="tableheader">Entry</td>
	    <td id="tableheader"><label for="doortype">Entry Type</label></td>
	    <td id="tableheader"><label for="doorstatus">Entry Status</label></td>
      </tr>
      
      <?php
	  $doorsql = "SELECT * FROM entry WHERE enclosureId = ".$cageid;
      $entrynumber = 1;
	  foreach($dbh->query($doorsql) as $row)
		{
      	echo "<tr>";
	    echo "<td>". $entrynumber . "</td>";
		
		echo "<td>
		<select name='doortype".$entrynumber."' id='doortype".$entrynumber."'>";
		echo '<option value="1"';
		if ($row[2] == 1) {echo "selected='selected'";}
		echo '>Single Door</option>';
		
		echo '<option value="2"';
		if ($row[2] == 2) {echo "selected='selected'";}
		echo '>Double Door</option>';
		
		echo '<option value="3"';
		if ($row[2] == 3) {echo "selected='selected'";}
		echo '>Hatch Door</option>';
		echo "</select></td>";
		
		$row[3];
		
		echo "<td>
		<select name='doorstatus".$entrynumber."' id='doorstatus".$entrynumber."'>";
		echo '<option value="1"';
		if ($row[3] == 1) {echo "selected='selected'";}
		echo '>Open</option>';
		
		echo '<option value="0"';
		if ($row[3] == 0) {echo "selected='selected'";}
		echo '>Closed</option>';
		echo "</select></td>";
			
		echo "<input type='hidden' name='entryid' value='".$row[0] . "' />";
		echo "<td>". "<input type='submit' name='submit' id='submit' value='Delete' />" . "</td>";

      echo "</tr>";
	  $entrynumber+=1;
	  }
	  echo '<input type="hidden" id="numentry" name="numentry" value="'.$numberofEntries.'" />';
	  	  echo '<input type="hidden" id="cageid" name="cageid" value="'.$cageid.'" />';
	  $dbh = null;?>
</table>
<input type="submit" name="submit" id="submit" value="Edit Cage Entries" />
<input type="submit" name="submit" id="submit" value="Return to Administator page" />
</form>
</body>
</html>